/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.entities;

import ru.ssau.lib.PresentableAsStringIntegerPair;
import java.io.Serializable;
import java.util.Date;
import org.jdom.Element;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.XMLHelper;

public class Message implements Serializable, PresentableAsStringIntegerPair, XMLizable {

    private int id;
    private String caption;
    private String text;
    private Integer userId;
    private Integer threadId;
    private Date postDate;

    public Message(int id, String caption, String text, Integer userId,
            Integer threadId, Date postDate) {
        this.id = id;
        this.caption = caption;
        this.text = text;
        this.userId = userId;
        this.threadId = threadId;
        this.postDate = postDate;
    }

    public Element toXMLElement() {
        Element root = new Element("Message");
        XMLHelper xhelp = new XMLHelper();
        xhelp.addElement(root, "id", id);
        xhelp.addElement(root, "caption", caption);
        xhelp.addElement(root, "text", text);
        xhelp.addElement(root, "userId", userId);
        xhelp.addElement(root, "threadId", threadId);
        xhelp.addElement(root, "postDate", postDate);
        return root;
    }

    public void loadFromXMLElement(Element data) {
        Element temp = null;
        if ((temp = data.getChild("id")) != null) {
            setId(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("caption")) != null) {
            setCaption(temp.getValue());
        }
        if ((temp = data.getChild("text")) != null) {
            setText(temp.getValue());
        }
        if ((temp = data.getChild("userId")) != null) {
            if(temp.getValue().contains("null")) {
                setUserId(null);
            } else {
                setUserId(Integer.valueOf(temp.getValue()));
            }
        }
        if ((temp = data.getChild("threadId")) != null) {
            setThreadId(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("postDate")) != null) {
            setPostDate(new Date(Long.valueOf(temp.getValue())));
        }
    }

    public Message() {
        caption = "";
        text = "";
        postDate = null;
        userId = -1;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getThreadId() {
        return threadId;
    }

    public void setThreadId(Integer threadId) {
        this.threadId = threadId;
    }

    public Date getPostDate() {
        return postDate;
    }

    public void setPostDate(Date postDate) {
        this.postDate = postDate;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != Message.class) {
            return false;
        }
        Message other = (Message) obj;

        boolean userIdEq = true;
        if ((userId == null && other.userId != null) || (userId != null && other.userId == null)) {
            userIdEq &= false;
        }
        if (userId == null && other.userId == null) {
            userIdEq &= true;
        }

        return (id == other.id && userIdEq
                && threadId.equals(other.threadId) && text.equals(other.text)
                && caption.equals(other.caption)) && postDate.equals(other.postDate);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + this.id;
        hash = 37 * hash + (this.caption != null ? this.caption.hashCode() : 0);
        hash = 37 * hash + (this.text != null ? this.text.hashCode() : 0);
        hash = 37 * hash + (this.userId != null ? this.userId.hashCode() : 0);
        hash = 37 * hash + (this.threadId != null ? this.threadId.hashCode() : 0);
        hash = 37 * hash + (this.postDate != null ? this.postDate.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", caption=");
        sb.append(caption);
        sb.append(", text=");
        sb.append(text);
        sb.append(", userId=");
        sb.append(userId);
        sb.append(", threadId=");
        sb.append(threadId);
        sb.append(", postDate=[");
        sb.append(postDate.toString());
        sb.append("]]");
        return sb.toString();
    }

    public StringIntegerPair toStringIntegerPair() {
        return new StringIntegerPair(id, caption);
    }
}
